package edu.friday.controller;

import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.model.SysUser;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.ISysRoleService;
import edu.friday.service.ISysUserService;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/user")
@Validated
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:user:query')")
    public TableDataInfo selectAllUser(SysUserVO sysUserVO, Pageable pageable) {
        int pageNumber = pageable.getPageNumber() - 1;
        pageNumber = Math.max(pageNumber, 0);
        pageable = PageRequest.of(pageNumber, pageable.getPageSize());
        return iSysUserService.selectUserList(sysUserVO, pageable);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('system:user:add')")
    public RestResult add(@RequestBody @Validated SysUserVO sysUserVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkUsernameUnique(sysUserVO.getUserName())))
            return RestResult.error(sysUserVO.getUserName() + "账户已存在");
        else if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkPhoneUnique(sysUserVO)))
            return RestResult.error(sysUserVO.getPhonenumber() + "电话号码已存在");
        else if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkEmailUnique(sysUserVO)))
            return RestResult.error(sysUserVO.getEmail() + "电子邮箱已存在");
        sysUserVO.setCreateBy("system");
        return toAjax(iSysUserService.insertUser(sysUserVO) ? 1 : 0);
    }

    @DeleteMapping("/{userIds}")
    @PreAuthorize("hasAuthority('system:user:remove')")
    public RestResult deleteUser(@PathVariable Long[] userIds) {
        return toAjax(iSysUserService.deleteUserByIds(userIds));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('system:user:edit')")
    public RestResult edit(@RequestBody @Validated SysUserVO sysUserVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkPhoneUnique(sysUserVO)))
            return RestResult.error(sysUserVO.getPhonenumber() + "电话号码已存在");
        else if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkEmailUnique(sysUserVO)))
            return RestResult.error(sysUserVO.getEmail() + "Email已存在");
        sysUserVO.setCreateBy("system");
        return toAjax(iSysUserService.updateUser(sysUserVO) ? 1 : 0);
    }

    @GetMapping(value = {"/", "/{userId}"})
    @PreAuthorize("hasAuthority('system:user:query')")
    public RestResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        RestResult result = RestResult.success();
        result.put("roles",iSysRoleService.selectRoleAll());
        if (StringUtils.isNotNull(userId)) {
            SysUser sysUser = iSysUserService.selectUserById(userId);
            List<Long> list = iSysRoleService.selectRoleListByUserId(userId);
            result.put(RestResult.DATA_TAG,sysUser);
            result.put("roleIds",list);
        }
        return result;
    }
}
